package com.aiyige.im;

import android.text.TextUtils;

import com.aiyige.MyApp;
import com.aiyige.R;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.im.model.SystemMessageModel;
import com.aiyige.im.model.UserIdModel;
import com.aiyige.share.model.ShareContactItem;
import com.aiyige.utils.ListUtil;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nimlib.sdk.InvocationFuture;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.RecentContact;
import com.netease.nimlib.sdk.uinfo.UserService;
import com.netease.nimlib.sdk.uinfo.model.NimUserInfo;
import com.netease.nimlib.sdk.uinfo.model.UserInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 自定义最近联系人助手
 * xiachao
 */
public class RecentContactHelper {

    public static RecentContactHelper getInstance() {
        return instance;
    }

    final static RecentContactHelper instance = new RecentContactHelper();


    public static List<SystemMessageModel> getSystemList() {
        List<SystemMessageModel> list = new ArrayList<>();
        list.add(SystemMessageModel.newBuilder().type(SystemMessageModel.type_system).title(MyApp.getAppContext().getString(R.string.system_notify)).headResId(R.drawable.news_system).build());
        list.add(SystemMessageModel.newBuilder().type(SystemMessageModel.type_interaction).title(MyApp.getAppContext().getString(R.string.interaction_notify)).headResId(R.drawable.news_like).build());
        list.add(SystemMessageModel.newBuilder().type(SystemMessageModel.type_order).title(MyApp.getAppContext().getString(R.string.order_notify)).headResId(R.drawable.news_order).build());
        return list;
    }


    public void setrecentContactList(List<RecentContact> list) {
        this.recentContactList = list;
    }

    public void cleanRecentContactList() {
        if (recentContactList != null) {
            recentContactList.clear();
        }
    }

    List<RecentContact> recentContactList;

    /**
     * 获取最近联系人(除去了系统消息和群聊，只保留P2P)
     * @return
     */
    public List<RecentContact> getRecentContactList() {
        List<RecentContact> ignoreSystemList = new ArrayList<>();
        if (!ListUtil.isEmpty(recentContactList)) {
            for (RecentContact recentContact : recentContactList) {
                if (recentContact.getSessionType() == SessionTypeEnum.P2P) {
                    ignoreSystemList.add(recentContact);
                }
            }
        }
        return ignoreSystemList;
    }

    /**
     * 获取最近可分享的联系人
     * @return
     */
    public List<ShareContactItem> getShareContactList(int maxNumber) {
        List<ShareContactItem> ignoreSystemList = new ArrayList<>();
        if (!ListUtil.isEmpty(recentContactList)) {
            for (RecentContact recentContact : recentContactList) {
                if (recentContact.getSessionType() == SessionTypeEnum.P2P) {
                    ignoreSystemList.add(ShareContactItem.newBuilder().accid(recentContact.getContactId())
                    .avatar(getAvatar(recentContact.getContactId()))
                    .name(getName(recentContact.getContactId()))
                    .type(ShareContactItem.TYPE_NORMAL).build());
                    if (ignoreSystemList.size() >= maxNumber) {
                        break;
                    }
                }
            }
        }
        return ignoreSystemList;
    }

    public UserInfo getUserInfo(String account) {
        if (!TextUtils.isEmpty(account)) {
            UserInfo userInfo = NimUIKit.getUserInfoProvider().getUserInfo(account);
            return userInfo;
        }
        return null;
    }

    public String getAvatar(String account) {
        if (!TextUtils.isEmpty(account)) {
            UserInfo userInfo = NimUIKit.getUserInfoProvider().getUserInfo(account);
            return userInfo == null ? "" : userInfo.getAvatar();
        }
        return null;
    }

    public String getName(String account) {
        if (!TextUtils.isEmpty(account)) {
            UserInfo userInfo = NimUIKit.getUserInfoProvider().getUserInfo(account);
            return userInfo == null ? "" : userInfo.getName();
        }
        return "";
    }

    public NimUserInfo getNimUserInfo(String account,final RequestUserIdCallback callback) {
        InvocationFuture<List<NimUserInfo>> userInfoRemotes =  NIMClient.getService(UserService.class).fetchUserInfo(Arrays.asList(account));
        if (userInfoRemotes != null) {
            userInfoRemotes.setCallback(new RequestCallback<List<NimUserInfo>>() {
                @Override
                public void onSuccess(List<NimUserInfo> nimUserInfos) {
                    if (!ListUtil.isEmpty(nimUserInfos)) {
                        NimUserInfo nimUserInfo = nimUserInfos.get(0);
                        String json  = nimUserInfo.getExtension();
                        if (!TextUtils.isEmpty(json)) {
                            UserIdModel userIdModel = JsonUtil.toObject(json,UserIdModel.class);
                            if (userIdModel != null && callback != null && !TextUtils.isEmpty(userIdModel.getUserId())) {
                                callback.onSuccess(userIdModel.getUserId());
                            }
                        }
                    }
                }

                @Override
                public void onFailed(int i) {

                }

                @Override
                public void onException(Throwable throwable) {

                }
            });
        }
        return null;
    }

    public interface RequestUserIdCallback{
        public void onSuccess(String userId);
    }
}
